<?php

 /**
  * @package JooS
  * @subpackage PHPackager_FS
  */

  abstract class JooS_PHPackager_FS_Entity {
    const ENTITY_NONE = "None";

    const ENTITY_FILE = "File";

    const ENTITY_DIR = "Dir";

    const ENTITY_UNKNOWN = "Unknown";

    private $_path = null;

    private $_virtual = false;

    abstract public function file_exists();

    abstract public function is_dir();

    abstract public function is_file();

    abstract public function is_readable();

    abstract public function is_writable();

    /**
     * @return string
     */
    public function basename() {
      return basename($this->getPath());
    }

    /**
     * @return string
     */
    public function dirname() {
      return dirname($this->getPath());
    }

    /**
     * @param string $path
     * @param string $type
     * @return string JooS_PHPackager_FS_Entity
     */
    final public static function getInstance($path, $type = null) {
      $className = __CLASS__ . "_";

      if ($type !== null)
        $className .= $type;
      elseif (!file_exists($path))
        $className .= self::ENTITY_NONE;
      elseif (is_file($path))
        $className .= self::ENTITY_FILE;
      elseif (is_dir($path))
        $className .= self::ENTITY_DIR;
      else
        $className .= self::ENTITY_UNKNOWN;

      require_once "JooS/Loader.php";

      if (JooS_Loader::loadClass($className)) {
        $entity = new $className($path);
        if ($type !== null)
          $entity->_virtual = true;
      }
      else
        $entity = null;

      return $entity;
    }

    /**
     * @param string $path
     */
    protected function __construct($path) {
      $this->_path = $path;
    }

    /**
     * @return string
     */
    public function getPath() {
      return $this->_path;
    }

    /**
     * @return bool
     */
    public function isVirtual() {
      return $this->_virtual;
    }
  }
